#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>

const int N = 305, M = 90005;
int n, m, aptr = 1, f[M], ans[100005];

inline int read() {
  int x = 0, ch = getchar(), flag = 0;
  while (!isdigit(ch)) {
    if (ch == '-') flag = 1;
    ch = getchar();
  }
  while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return flag ? -x : x;
}

struct Market {
  int c, v, t;
  inline bool operator<(const Market& rhs) const { return t < rhs.t; }
} a[N];

struct Query {
  int t, m, id;
  inline bool operator<(const Query& rhs) const { return t < rhs.t; }
} q[100005];

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("market.in", "r", stdin);
  freopen("market.out", "w", stdout);
#endif
#endif

  n = read(), m = read();
  for (register int i = 1; i <= n; ++i) a[i].c = read(), a[i].v = read(), a[i].t = read();
  std::sort(a + 1, a + n + 1);
  for (register int i = 1; i <= m; ++i) q[i].t = read(), q[i].m = read(), q[i].id = i;
  std::sort(q + 1, q + m + 1);
  memset(f, 0x3f, sizeof(f));
  f[0] = 0;
  for (register int i = 1; i <= m; ++i) {
    while (aptr <= n && q[i].t >= a[aptr].t) {
      for (register int j = M - 1; j >= a[aptr].v; --j)
        f[j] = std::min(f[j], f[j - a[aptr].v] + a[aptr].c);
      aptr++;
      for (register int j = M - 2; j; --j) f[j] = std::min(f[j], f[j + 1]);
    }
    ans[q[i].id] = std::upper_bound(f + 1, f + M, q[i].m) - f - 1;
  }
  for (register int i = 1; i <= m; ++i) printf("%d\n", ans[i]);
  return 0;
}